public class T {


    public static void mergeSort(int[] arr){


        mergeSortTem( arr,0,arr.length-1);
    }

    private static void mergeSortTem(int[] arr, int left, int right) {

        if(left>=right){
            return;
        }

        int mid=(left+right)/2;
        mergeSortTem(arr,left,mid);
        mergeSortTem(arr,mid+1,right);

        merge(arr,left,right,mid);
    }

    private static void merge(int[] arr, int left, int right,int mid) {
        int[] tmp=new int[right-left+1];
        int s1=left;
        int s2=mid+1;
        int k=0;

        while(s1<=mid&&s2<=right){
            if(arr[s1]>arr[s2]){
                tmp[k++]=arr[s2++];
            }else {
                tmp[k++]=arr[s1++];
            }
        }

        while(s1<=mid){
            tmp[k++]=arr[s1++];
        }

        while(s2<right){
            tmp[k++]=arr[s2++];
        }

        for (int i = 0; i < k; i++) {
            arr[i+left]=tmp[i];
        }



    }

    public static void mergeSortNor(int[] arr){

        int gap=1;
        while (gap<arr.length){
            for (int i = 0; i < arr.length; i+=gap*2) {
                int mid=i+gap-1;
                int left=i;
                if (mid>=arr.length){
                    mid=arr.length-1;
                }

                int right=mid+gap;
                if(right>=arr.length){
                    right=arr.length-1;
                }

                merge(arr,left,right,mid);



            }
        }


    }


}


